<template>
	<view class="record-container">
		<!-- 顶部标题 -->
		<view class="page-header">
			<text class="page-title">考试记录</text>
		</view>
		
		<!-- 考试记录列表 -->
		<scroll-view class="record-list" scroll-y @scrolltolower="loadMore" lower-threshold="100">
			<view class="record-item" v-for="item in recordList" :key="item.id" @click="goToDetail(item)">
				<view class="record-header">
					<view class="exam-title">{{ item.title }}</view>
					<view class="exam-tag" :class="item.statusClass">{{ item.status }}</view>
				</view>
				
				<view class="record-info">
					<view class="info-row">
						<text class="label">{{ item.subject }}</text>
						<text class="label">{{ item.type }}</text>
					</view>
					<view class="info-row">
						<text class="info-text">对错:{{ item.correct }}/{{ item.total }}</text>
						<text class="info-text">得分:{{ item.score }}/{{ item.totalScore }}</text>
						<text class="info-text">耗时: {{ item.duration }}</text>
					</view>
				</view>
				
				<view class="record-footer">
					<text class="time">{{ item.time }}</text>
					<view class="action-btns">
						<button class="btn btn-view" @click.stop="viewPaper(item)">查看试卷</button>
						<button class="btn btn-correct" v-if="item.needCorrect" @click.stop="correctPaper(item)">去批改</button>
					</view>
				</view>
			</view>
			
			   <!-- 空状态 -->
			   <view class="empty-state" v-if="recordList.length === 0">
				   <image src="/image/dingdan.png" mode="aspectFit"></image>
				   <text>暂无考试记录</text>
			   </view>
			   <!-- 加载更多提示 -->
			   <view v-if="recordList.length > 0" style="text-align:center;padding:30rpx 0;color:#999;">
				   <text v-if="loadingMore">加载中...</text>
				   <text v-else-if="noMore">没有更多了</text>
				   <text v-else>上拉加载更多</text>
			   </view>
		</scroll-view>
	</view>
</template>

<script>
import { api } from '@/http.js'

export default {
	data() {
		   return {
			   recordList: [],
			   loading: false,
			   isLogin: false,
			   pageNum: 1,
			   pageSize: 10,
			   noMore: false,
			   loadingMore: false
		   }
	},
	onShow() {
		// 检查登录状态
		const token = uni.getStorageSync('token');
		this.isLogin = !!token;
		console.log('=== record.vue onShow ===');
		console.log('isLogin:', this.isLogin);
		
		   if (this.isLogin) {
			   this.pageNum = 1;
			   this.noMore = false;
			   this.recordList = [];
			   this.getRecordList();
		   } else {
			uni.showToast({
				title: '请先登录',
				icon: 'none'
			});
		}
	},
	methods: {
		   async getRecordList() {
			   this.loadingMore = true;
			   try {
				   console.log('=== 开始获取考试记录 ===');
				   const res = await api.getExamRecordList({ 
					   pageIndex: this.pageNum, 
					   pageSize: this.pageSize 
				   });
				   console.log('考试记录API响应状态码:', res?.code);
				   console.log('考试记录API响应消息:', res?.message);
				   if (res.response && res.response.list && res.response.list.length > 0) {
					   console.log('第一条记录完整数据:', JSON.stringify(res.response.list[0], null, 2));
				   }
				   if (res && res.code === 1) {  // 文档中成功标志是 code === 1
					   const list = (res.response && res.response.list) ? res.response.list.map(item => ({
						   id: item.id,
						   title: item.paperName,  // 使用 paperName
						   subject: item.subjectName || '未知',  // 使用 subjectName
						   type: item.paperTypeStr || '未知',  // 使用 paperTypeStr
						   status: item.status === 1 ? '已完成' : (item.status === 2 ? '已阅卷' : '待批改'),
						   statusClass: item.status === 1 ? 'view' : (item.status === 2 ? 'checked' : 'pending'),
						   correct: item.questionCorrect || 0,  // 使用 questionCorrect
						   total: item.questionCount || 0,  // 使用 questionCount
						   score: item.userScore || 0,  // 使用 userScore
						   totalScore: item.paperScore || 0,  // 使用 paperScore
						   duration: item.doTime || '未知',  // 直接使用 doTime (已经是格式化的)
						   time: item.createTime || new Date().toLocaleString(),
						   needCorrect: item.status === 1  // status=1表示待批改
					   })) : [];
					   if (this.pageNum === 1) {
						   this.recordList = list;
					   } else {
						   this.recordList = this.recordList.concat(list);
					   }
					   // 判断是否还有更多
					   if (!list || list.length < this.pageSize) {
						   this.noMore = true;
					   }
					   console.log('考试记录更新成功:', this.recordList.length, '条');
				   } else {
					   if (this.pageNum === 1) this.recordList = [];
					   this.noMore = true;
					   console.error('API返回错误:', res);
					   uni.showToast({
						   title: res?.message || '获取记录失败',
						   icon: 'none'
					   });
				   }
			   } catch (err) {
				   if (this.pageNum === 1) this.recordList = [];
				   this.noMore = true;
				   console.error('获取考试记录失败:', err);
				   uni.showToast({
					   title: '加载失败，请稍后重试',
					   icon: 'none'
				   });
			   } finally {
				   this.loadingMore = false;
			   }
		   },
		goToDetail(item) {
			// 使用答卷ID查看已提交的答卷
			uni.navigateTo({
				url: `/pages/exam/detail?recordId=${item.id}&mode=view&source=record`
			});
		},
		viewPaper(item) {
			// 查看已提交的答卷详情
			uni.navigateTo({
				url: `/pages/exam/detail?recordId=${item.id}&mode=view&source=record`
			});
		},
		correctPaper(item) {
			// 批改试卷
			uni.navigateTo({
				url: `/pages/exam/detail?recordId=${item.id}&mode=correct&source=record`
			});
		   },
		   loadMore() {
			   if (this.loadingMore || this.noMore) return;
			   this.pageNum++;
			   this.getRecordList();
		   }
	   }
	}
</script>

<style lang="scss" scoped>
.record-container {
	min-height: 100vh;
	background: #FFF5F7;
	
	.page-header {
		background: linear-gradient(135deg, #FFB6C1 0%, #FFC0CB 100%);
		padding: 30rpx;
		text-align: center;
		
		.page-title {
			font-size: 36rpx;
			font-weight: bold;
			color: #fff;
		}
	}
	
	.record-list {
		height: calc(100vh - 100rpx);
		padding: 0;
		
		.record-item {
			margin: 20rpx 30rpx 0 30rpx;
			width: calc(100% - 60rpx);
			background: #fff;
			border-radius: 20rpx;
			padding: 30rpx;
			box-sizing: border-box;
			box-shadow: 0 4rpx 20rpx rgba(255, 182, 193, 0.15);
			
			.record-header {
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-bottom: 20rpx;
				
				.exam-title {
					font-size: 32rpx;
					font-weight: bold;
					color: #333;
					flex: 1;
				}
				
				.exam-tag {
					padding: 8rpx 20rpx;
					border-radius: 30rpx;
					font-size: 24rpx;
					font-weight: 500;
					
					&.view {
						background: #E0F7FA;
						color: #00BCD4;
					}
					
					&.pending {
						background: #FFE4E1;
						color: #FF69B4;
					}
				}
			}
			
			.record-info {
				margin-bottom: 20rpx;
				
				.info-row {
					display: flex;
					align-items: center;
					margin-bottom: 12rpx;
					
					&:last-child {
						margin-bottom: 0;
					}
					
					.label {
						padding: 6rpx 16rpx;
						background: #FFF0F5;
						color: #FFB6C1;
						font-size: 24rpx;
						border-radius: 20rpx;
						margin-right: 15rpx;
					}
					
					.info-text {
						font-size: 24rpx;
						color: #666;
						margin-right: 20rpx;
					}
				}
			}
			
			.record-footer {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding-top: 20rpx;
				border-top: 2rpx solid #FFF0F5;
				
				.time {
					font-size: 24rpx;
					color: #999;
				}
				
				.action-btns {
					display: flex;
					gap: 15rpx;
					
					.btn {
						padding: 12rpx 25rpx;
						border-radius: 30rpx;
						font-size: 24rpx;
						border: none;
						line-height: 1;
						
						&.btn-view {
							background: #E0F7FA;
							color: #00BCD4;
						}
						
						&.btn-correct {
							background: #FFE4E1;
							color: #FF69B4;
						}
					}
				}
			}
		}
		
		.empty-state {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			margin: 200rpx 30rpx;
			padding: 0;
			width: calc(100% - 60rpx);
			box-sizing: border-box;
			
			image {
				width: 200rpx;
				height: 200rpx;
				margin-bottom: 30rpx;
				opacity: 0.3;
			}
			
			text {
				font-size: 28rpx;
				color: #999;
			}
		}
	}
}
</style>
